package com.lineage.server.model.doll;
import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.sound.midi.InvalidMidiDataException;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.DollPowerTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.Instance.L1DollInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.doll.L1DollExecutor;
import com.lineage.server.model.item.L1ItemId;
import com.lineage.server.serverpackets.ServerBasePacket;
//import com.lineage.server.serverpackets.doll.S_DollCompoundInit;
//import com.lineage.server.serverpackets.doll.S_DollCompoundItem;
//import com.lineage.server.serverpackets.doll.S_DollCompoundMaterial;
//import com.lineage.server.serverpackets.doll.S_DollCompoundRoll;
import com.lineage.server.templates.L1Doll;
import com.lineage.server.templates.L1Item;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.timecontroller.pet.DollAidTimer;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldDoll;
import com.lineage.server.world.WorldItem;


public class L1DollPowerSupper {
	private static final Log _log = LogFactory.getLog(L1DollPowerSupper.class);
	private static final HashMap<Integer, L1DollExecutor> _classList = new HashMap<Integer, L1DollExecutor>();
	private static L1DollPowerSupper _instance;
	
	private static final ArrayList<String> _checkList = new ArrayList<String>();
	
	public HashMap<Integer, L1DollExecutor> getClasslist() {
		return _classList;
	}
	
	public static L1DollPowerSupper get() {
		if (_instance == null) {
			_instance = new L1DollPowerSupper();
		}
		return _instance;
	}
	
	public void loadPowers() {
		PerformanceTimer timer = new PerformanceTimer();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();

			pstm = con.prepareStatement("SELECT * FROM `etcitem_doll_power_supper`");

			rs = pstm.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("id");
				String classname = rs.getString("classname");
				int type1 = rs.getInt("type1");
				int type2 = rs.getInt("type2");
				int type3 = rs.getInt("type3");
				String note = rs.getString("note");
				// 新增娃娃能力描述↓↓↓
				final int doll_ac = rs.getInt("ac");
				final int doll_str = rs.getInt("str");
				final int doll_dex = rs.getInt("dex");
				final int doll_con = rs.getInt("con");
				final int doll_intel = rs.getInt("int");
				final int doll_wis = rs.getInt("wis");
				final int doll_cha = rs.getInt("cha");
				final int doll_hp = rs.getInt("hp");
				final int doll_mp = rs.getInt("mp");
				final int doll_hpr = rs.getInt("hpr");
				final int doll_mpr = rs.getInt("mpr");
				final int doll_mr = rs.getInt("mr");
				final int doll_sp = rs.getInt("sp");
				final int doll_dmg = rs.getInt("dmg");
				final int doll_hit = rs.getInt("hit");
				final int doll_bowdmg = rs.getInt("bowdmg");
				final int doll_bowhit = rs.getInt("bowhit");
				final int doll_alldmg_r = rs.getInt("alldmg_r");
				final int doll_exp = rs.getInt("exp");
				final int doll_weight = rs.getInt("weight");
				final int doll_weight_r = rs.getInt("weight_r");
				final int doll_earth = rs.getInt("earth");
				final int doll_wind = rs.getInt("wind");
				final int doll_water = rs.getInt("water");
				final int doll_fire = rs.getInt("fire");
				final int doll_stun = rs.getInt("stun");
				final int doll_stone = rs.getInt("stone");
				final int doll_sleep = rs.getInt("sleep");
				final int doll_freeze = rs.getInt("freeze");
				final int doll_sustain = rs.getInt("sustain");
				final int doll_blind = rs.getInt("blind");
				final int doll_haste = rs.getInt("haste");
				final int doll_stunlv = rs.getInt("stunlv");
				final int doll_breaklv = rs.getInt("breaklv");
				final int doll_foeslayer = rs.getInt("foeslayer");
				final int doll_titanhp = rs.getInt("titanhp");
				// 新增娃娃能力描述↑↑↑
				String ch = classname + "=" + type1 + "=" + type2 + "=" + type3 + "=" + note;
				

				if (_checkList.lastIndexOf(ch) == -1 || _classList.size() == 0 ) {
					_checkList.add(ch);

					// 新增娃娃能力描述↓↓↓
					addList(id, 
							classname,
							type1, 
							type2, 
							type3, 
							note,
							doll_ac,
							doll_str,
							doll_dex,
							doll_con,
							doll_intel,
							doll_wis,
							doll_cha,
							doll_hp,
							doll_mp,
							doll_hpr,
							doll_mpr,
							doll_mr,
							doll_sp,
							doll_dmg,
							doll_hit,
							doll_bowdmg,
							doll_bowhit,
							doll_alldmg_r,
							doll_exp,
							doll_weight,
							doll_weight_r,
							doll_earth,
							doll_wind,
							doll_water,
							doll_fire,
							doll_stun,
							doll_stone,
							doll_sleep,
							doll_freeze,
							doll_sustain,
							doll_blind,
							doll_haste,
							doll_stunlv,
							doll_breaklv,
							doll_foeslayer,
							doll_titanhp
							);
//					// 新增娃娃能力描述↑↑↑
				} else {
					_log.error("娃娃能力设置重复:id=" + id + " type1=" + type1 + " type2=" + type2 + " type3=" + type3);
				}
			}
		} catch (SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.error(e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
//			_classList.clear();
		}
		_log.info("读取->娃娃能力资料数量: " + _classList.size() + "(" + timer.get() + "ms)");	
	}

	
	
	
//	public void loadSupper() {
//		
//		Connection cn = null;
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			cn = DatabaseFactory.get().getConnection();
//			ps = cn.prepareStatement("select id, powers from `doll_power_supper`");
//			rs = ps.executeQuery();
//			boolean b = false;
//			while (rs.next()) {
//				int id = rs.getInt("id");
//				
//				if (id == targObjId) {
//					b = true;
//				}					
//			}
//
//		} catch (Exception e) {
//			_log.error(e.getLocalizedMessage(), e);
//		}finally {
//			SQLUtil.close(rs);
//			SQLUtil.close(ps);
//			SQLUtil.close(cn);
//		}
//	}

	
	public void resetDollType(int itemObjId, L1Doll type) {
//		int did = itemObjId;
		if ( _checkList.size() == -1 ||  _classList.size() == 0 ) {
			loadPowers();
		}
		

		L1Doll _type =  type;
		
		
		Connection cn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			cn = DatabaseFactory.get().getConnection();
			ps = cn.prepareStatement("SELECT ch.id, ch.item_id, dp.powers "
					+ "FROM character_items as ch join doll_power_supper as dp on ch.id = dp.id ;");
			rs = ps.executeQuery();

			while (rs.next()) {
				int pObjId = rs.getInt("id");
				int itemid = rs.getInt("item_id");
				String dollPowers = rs.getString("powers").replaceAll(" ", "");	

				boolean iserr = false;
				if (pObjId == itemObjId) {
					_type.getPowerList().clear();
					ArrayList<L1DollExecutor> powerList = new ArrayList<L1DollExecutor>();
					if ((dollPowers != null) && (!dollPowers.equals(""))) {
						String[] dollPowerSet1 = dollPowers.split(",");
						for (String string : dollPowerSet1) {
							L1DollExecutor e = (L1DollExecutor) _classList.get(Integer.valueOf(Integer.parseInt(string)));

							if (e != null) {
								//这里添加娃娃的能力值
							
								powerList.add(e);
							} else {
								_log.error("娃娃能力取回错误-没有这个编号,是这里么:" + string);
								iserr = true;
							}
						}
					}
					
					_type.setPowerList(powerList);			
				}		
			}
		} catch (SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(ps);
			SQLUtil.close(cn);
		}
	}
	
	
	
	public void resetDollType() {
//		int did = itemObjId;
		if ( _checkList.size() == -1 ||  _classList.size() == 0 ) {
			loadPowers();
		}
	
		Connection cn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			cn = DatabaseFactory.get().getConnection();
			ps = cn.prepareStatement("SELECT ch.id, ch.item_id, dp.powers "
					+ "FROM character_items as ch join doll_power_supper as dp on ch.id = dp.id ;");
			rs = ps.executeQuery();

			while (rs.next()) {
				int pObjId = rs.getInt("id");
				int itemid = rs.getInt("item_id");
				String dollPowers = rs.getString("powers").replaceAll(" ", "");	
				L1Doll doll = new L1Doll();
				boolean iserr = false;
				
				ArrayList<L1DollExecutor> powerList = new ArrayList<L1DollExecutor>();
				if ((dollPowers != null) && (!dollPowers.equals(""))) {
					String[] dollPowerSet1 = dollPowers.split(",");
					for (String string : dollPowerSet1) {
						L1DollExecutor e = (L1DollExecutor) _classList.get(Integer.valueOf(Integer.parseInt(string)));

						if (e != null) {
							//这里添加娃娃的能力值
						
							powerList.add(e);
						} else {
							_log.error("娃娃能力取回错误-没有这个编号,是这里么:" + string);
							iserr = true;
						}
					}
				}
				
				Collection<L1ItemInstance> l1Item =   WorldItem.get().all();
				for (L1ItemInstance it : l1Item) {
					if (it.getItemId() == itemid) {
						 doll = DollPowerTable.get().get_type(itemid);
						 if (doll != null) {
							 doll.setPowerList(powerList);	
						}
					}
				}
	

					
							
					
			}
		} catch (SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(ps);
			SQLUtil.close(cn);
		}
	}
	
	
	public static void delete(final int itemid) {
		Connection cn = null;
		PreparedStatement ps = null;
		try {
			cn = DatabaseFactory.get().getConnection();
			ps = cn.prepareStatement("DELETE FROM `doll_power_supper` WHERE `itemid`=?");
			ps.setInt(1, itemid);
			ps.execute();

		} catch (final SQLException e) {
			_log.error(e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(ps);
			SQLUtil.close(cn);
		}
	}
	
	
	private void addList(final int powerid, final String className, final int int1, final int int2, final int int3,
			final String note,
			final int doll_ac,
			final int doll_str,
			final int doll_dex,
			final int doll_con,
			final int doll_intel,
			final int doll_wis,
			final int doll_cha,
			final int doll_hp,
			final int doll_mp,
			final int doll_hpr,
			final int doll_mpr,
			final int doll_mr,
			final int doll_sp,
			final int doll_dmg,
			final int doll_hit,
			final int doll_bowdmg,
			final int doll_bowhit,
			final int doll_alldmg_r,
			final int doll_exp,
			final int doll_weight,
			final int doll_weight_r,
			final int doll_earth,
			final int doll_wind,
			final int doll_water,
			final int doll_fire,
			final int doll_stun,
			final int doll_stone,
			final int doll_sleep,
			final int doll_freeze,
			final int doll_sustain,
			final int doll_blind,
			final int doll_haste,
			final int doll_stunlv,
			final int doll_breaklv,
			final int doll_foeslayer,
			final int doll_titanhp
			) {
		// 新增娃娃能力描述↑↑↑
		if (className.equals("0"))
			return;
		try {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("com.lineage.server.model.doll.");
			stringBuilder.append(className);

			final Class<?> cls = Class.forName(stringBuilder.toString());
			final L1DollExecutor exe = (L1DollExecutor) cls.getMethod("get").invoke(null);
			exe.set_power(int1, int2, int3);
			exe.set_note(note);
			// 新增娃娃能力描述↓↓↓
			exe.setDollAc(doll_ac);
			exe.setDollStr(doll_str);
			exe.setDollDex(doll_dex);
			exe.setDollCon(doll_con);
			exe.setDollInt(doll_intel);
			exe.setDollWis(doll_wis);
			exe.setDollCha(doll_cha);
			exe.setDollHp(doll_hp);
			exe.setDollMp(doll_mp);
			exe.setDollHpr(doll_hpr);
			exe.setDollMpr(doll_mpr);
			exe.setDollMr(doll_mr);
			exe.setDollSp(doll_sp);
			exe.setDollDmg(doll_dmg);
			exe.setDollHit(doll_hit);
			exe.setDollBowDmg(doll_bowdmg);
			exe.setDollBowHit(doll_bowhit);
			exe.setDollAllDmg_R(doll_alldmg_r);
			exe.setDollExp(doll_exp);
			exe.setDollWeight(doll_weight);
			exe.setDollWeight_R(doll_weight_r);
			exe.setDollEarth(doll_earth);
			exe.setDollWind(doll_wind);
			exe.setDollWater(doll_water);
			exe.setDollFire(doll_fire);
			exe.setDollStun(doll_stun);
			exe.setDollStone(doll_stone);
			exe.setDollSleep(doll_sleep);
			exe.setDollFreeze(doll_freeze);
			exe.setDollSustain(doll_sustain);
			exe.setDollBlind(doll_blind);
			exe.setDollHaste(doll_haste);
			exe.setDollStunLv(doll_stunlv);
			exe.setDollBreakLv(doll_breaklv);
			exe.setDollFoeSlayer(doll_foeslayer);
			exe.setDollTiTanHp(doll_titanhp);
			// 新增娃娃能力描述↑↑↑

			_classList.put(new Integer(powerid), exe);
		} catch (ClassNotFoundException e) {
			String error = "发生[娃娃能力档案]错误, 检查档案是否存在:" + className + " 娃娃能力编号:" + powerid;
			_log.error(error);
		} catch (IllegalArgumentException e) {
			_log.error(e.getLocalizedMessage(), e);
		} catch (IllegalAccessException e) {
			_log.error(e.getLocalizedMessage(), e);
		} catch (InvocationTargetException e) {
			_log.error(e.getLocalizedMessage(), e);
		} catch (SecurityException e) {
			_log.error(e.getLocalizedMessage(), e);
		} catch (NoSuchMethodException e) {
			_log.error(e.getLocalizedMessage(), e);
		}
	}
	

}
